<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户编号" prop="userNo">
        <el-input
          v-model="queryParams.userNo"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="userType">
        <el-select v-model="queryParams.userType" placeholder="请选择" clearable>
          <el-option
            v-for="item in listRegisterClass"
            :key="item.id"
            :label="item.customerClass"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="会员等级" prop="memberGrade">
        <el-select v-model="queryParams.memberGrade" placeholder="请选择" clearable>
          <el-option
            v-for="item in listAllMemberGrade"
            :key="item.id"
            :label="item.gradeNameCn"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option label="正常" value="1"/>
          <el-option label="停用" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column label="用户编号" align="center" prop="userNo" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <!-- <el-table-column label="姓名" align="center" prop="userNameCn" /> -->
      <el-table-column label="电话号码" align="center" prop="phoneNumber" />
      <el-table-column label="工作邮箱" align="center" prop="email" />
      <el-table-column label="企业名称（英文）" align="center" prop="enterpriseName" width="140" />
      <el-table-column label="企业名称（中文）" align="center" prop="enterpriseNameCn" width="140"/>
      <el-table-column label="职务" align="center" prop="roleName" />
      <el-table-column label="国家-地区" align="center" prop="countryCn" />
      <el-table-column label="类型" align="center" prop="customerClassCn" />
      <el-table-column label="会员等级" align="center" prop="gradeNameCn" />
      <el-table-column label="会员有效期" align="center" prop="memberEndTime" />
      <el-table-column label="注册时间" align="center">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="用户角色" align="center" prop="roleName" />-->
      <el-table-column label="异常时间" align="center" prop="" />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <template v-if="scope.row.status=='1'">正常</template>
          <template v-if="scope.row.status=='2'">停用</template>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:customer:edit']"
          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-if="scope.row.status == 1"
            @click="handleFreezeOrUnFreeze(scope.row)"
            v-hasPermi="['system:customer:freezeOrUnFreeze']"
          >冻结</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-if="scope.row.status == 2"
            @click="handleFreezeOrUnFreeze(scope.row)"
            v-hasPermi="['system:customer:freezeOrUnFreeze']"
          >解冻</el-button>
          <el-button
            v-if="scope.row.isEnterprise == '0' && scope.row.isAdmin == '1' && scope.row.gradeCode!=='gjhy'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="setEnterpriseID(scope.row)"
            v-hasPermi="['system:customer:freezeOrUnFreeze']"
          >推荐企业号</el-button>
          <el-button
            v-else-if="scope.row.isEnterprise == '1' && scope.row.isAdmin == '1' && scope.row.gradeCode!=='gjhy'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="setEnterpriseID(scope.row)"
            v-hasPermi="['system:customer:freezeOrUnFreeze']"
          >取消企业号</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="openUpdateEnterprise(scope.row)"
            v-hasPermi="['system:customer:updateEnterprise']"
          >修改企业</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户端用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="用户名(真实姓名)" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名(真实姓名)" />
        </el-form-item>
        <el-form-item label="用户编号(会员编号)" prop="userNo">
          <el-input v-model="form.userNo" placeholder="请输入用户编号(会员编号)" />
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="电话号码" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入电话号码" />
        </el-form-item>
        <el-form-item label="国家中文" prop="countryCn">
          <el-input v-model="form.countryCn" placeholder="请输入国家中文" />
        </el-form-item>
        <el-form-item label="国家英文" prop="countryEn">
          <el-input v-model="form.countryEn" placeholder="请输入国家英文" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 修改用户企业对话框 -->
    <el-dialog title="修改用户企业" :visible.sync="openForUpdateEnterprise" width="500px" append-to-body>
      <el-form ref="formUpdateEnterprise" :model="formUpdateEnterprise" :rules="rules" label-width="80px">
        <el-form-item label="企业" prop="enterpriseId1">
          <el-select v-model="formUpdateEnterprise.enterpriseId" placeholder="请选择">
            <el-option
              v-for="item in listEnterprise"
              :key="item.id"
              :label="item.enterpriseName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateEnterprise()">确 定</el-button>
        <el-button @click="cancelUpdateEnterprise">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCustomer,
  getCustomer,
  delCustomer,
  addCustomer,
  updateCustomer,
  freezeOrUnFreeze,
  setEnterpriseID,
  updateEnterprise
} from "@/api/system/customer";
import Template from "@/views/system/mailTemplate";

import { getRegisterOption } from "@/api/system/register"

import { enterpriseListAllList } from "@/api/system/enterprise"
import { formatTime } from "@/utils/index"

import { listAllMemberGrade } from "@/api/system/memberGrade"

export default {
  name: "Customer",
  components: {Template},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户端用户表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseId: null,
        userName: null,
        password: null,
        userNo: null,
        phoneNumber: null,
        email: null,
        userType: null,
        countryCn: null,
        countryEn: null,
        address: null,
        slogan: null,
        avatar: null,
        isAdmin: null,
        customerRoleId: null,
        status: null,
        backgroundImage: null,
        memberGrade: null
      },
      // 表单参数
      form: {},
      // 更新企业表单参数
      formUpdateEnterprise: {
        enterpriseId: undefined
      },
      // 更新企业弹窗
      openForUpdateEnterprise: false,
      // 企业列表
      listEnterprise: [],
      // 表单校验
      rules: {
      },
      listRegisterClass: [],
      listAllMemberGrade: []
    };
  },
  created() {
    this.getList()
    this.getRegisterOption()
    this.getAllMemberGrade()
  },
  methods: {
    /** 查询客户端用户列表 */
    getList() {
      this.loading = true;
      listCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;

        if (this.customerList && this.customerList.length > 0) {
          for (let i = 0; i < this.customerList.length; i++) {

            // let data = this.customerList[i]
            // let time = data.createTime
            // if (time) {
            //   let createTime = formatTime(time, "yyyy-MM-dd HH:mm:ss")
            //   data.createTime = createTime
            // }

          }
        }

        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseId: null,
        userName: null,
        password: null,
        userNo: null,
        phoneNumber: null,
        email: null,
        userType: null,
        countryCn: null,
        countryEn: null,
        address: null,
        slogan: null,
        avatar: null,
        isAdmin: null,
        customerRoleId: null,
        status: null,
        backgroundImage: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        IsEnterprise: null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户端用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户端用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return delCustomer(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 冻结解冻
    handleFreezeOrUnFreeze(row) {

      const status = row.status

      let updateStatus, updateText;
      if (status == 1) {
        updateStatus = 2;
        updateText = "冻结";
      } else {
        updateStatus = 1;
        updateText = "启用";
      }

      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认' + updateText + '客户端用户数据项？').then(function() {
        return freezeOrUnFreeze({
          id: ids,
          status: updateStatus
        });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    // 设置/取消企业号
    setEnterpriseID(row) {
      let updateText;
      if (row.IsEnterprise == 0) {
        updateText = "设置";
      } else {
        updateText = "取消";
      }

      this.$modal.confirm('请确认是否' + updateText + '用户: ' + row.userName +' '+ row.enterpriseName+'公司管理员设置企业号.').then(function() {
        return setEnterpriseID(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    //
    openUpdateEnterprise(row) {
      this.openForUpdateEnterprise = true
      this.formUpdateEnterprise.enterpriseId = row.enterpriseId
      this.formUpdateEnterprise.id = row.id
      this.listEnterpriseListAll()
    },
    listEnterpriseListAll(){
      if (this.listEnterprise && this.listEnterprise.length > 0) {
        return
      }
      enterpriseListAllList().then(response => {
        this.listEnterprise = response.data
      });
    },
    updateEnterprise(){
      this.$refs['formUpdateEnterprise'].validate()
      updateEnterprise(this.formUpdateEnterprise).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.openForUpdateEnterprise = false
      })
    },
    cancelUpdateEnterprise() {
      this.openForUpdateEnterprise = false
    },
    getRegisterOption(){
      getRegisterOption().then(response => {
        this.listRegisterClass = response.data
      });
    },
    getAllMemberGrade(){
      listAllMemberGrade().then(response => {
        this.listAllMemberGrade = response.data
      });
    },

  }
};
</script>
